package com.yaorange.jk.web.cargo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Export;
import com.yaorange.jk.entity.Finance;
import com.yaorange.jk.entity.Invoice;
import com.yaorange.jk.entity.PackingList;
import com.yaorange.jk.service.ExportService;
import com.yaorange.jk.service.FinanceService;
import com.yaorange.jk.service.InvoiceService;
import com.yaorange.jk.service.PackingListService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;


@Controller("financeAction")
@Scope("prototype")
public class FinanceAction extends BaseAction implements ModelDriven<Finance>  {

    @Autowired
    private FinanceService financeService;
    
    @Autowired
    private PackingListService packingListService;
    
    
    @Autowired
    private InvoiceService invoiceService;
    
    @Autowired
    private ExportService exportService;

    private Pagination page=new Pagination();

    private List<Finance> financeList;

    private Finance model= new Finance();

    public String list(){

    Pagination pagination =  financeService.findPage(page);
    pagination.setUrl("financeAction_list");
    ActionContext.getContext().getValueStack().push(pagination);
    return "tolist";
    }

    public String tocreate(){
    financeList = financeService.findAll();
    //        ActionContext.getContext().getValueStack().set("financeList",financeList);
    page = invoiceService.findPage(page,1d);
    page.setUrl("financeAction_tocreate");
    push(page);
    return "tocreate";
    }

    public String insert(){
        financeService.save(model);
        return list();
    }

    public String toupdate(){
    Finance finance = financeService.findById(model.getId());
    super.push(finance);

    financeList = financeService.findAll();
    financeList.remove(finance);
    return "toupdate";
    }

    public String update(){
    financeService.update(model);
    return list();
    }
    
    public String finance(){
    	
    	String[] ids = model.getId().split(", ");
        for (String id:ids) {
        	Finance finance=financeService.findById(id);
        financeService.finance(finance);
        
        PackingList packingList = packingListService.findById(id);
    	String[] exportIds=packingList.getExportIds().split(", ");
    	for (String string : exportIds) {
			
    		Export export=exportService.findById(string);
    		export.setState(5); //设置export的状态为5，财务
    		exportService.update(export);
		}
        }
    	return list();
    }
    
public String cancel(){
    	
    	String[] ids = model.getId().split(", ");
        for (String id:ids) {
        	Finance finance=financeService.findById(id);
        	financeService.cancel(finance);
        	PackingList packingList = packingListService.findById(id);
        	String[] exportIds=packingList.getExportIds().split(", ");
        	for (String string : exportIds) {
				
        		Export export=exportService.findById(string);
        		export.setState(4); //设置export的状态为重置为4，发票
        		exportService.update(export);
			}
        }
    	return list();
    }
    
    
    /**
    * @return
    */
    public String delete(){
    String[] ids = model.getId().split(", ");
    for (String id:ids) {
    financeService.deleteById(model.getId());
    }

    return list();
    }

    public String toview()
    {
    Finance finance = financeService.findById(model.getId());
    push(finance);
    return "toview";
    }
    public void setPage(Pagination page) {
    this.page = page;
    }

    public List<Finance> getFinanceList() {
    return financeList;
    }

    @Override
    public Finance getModel() {
    return model;
    }
}
